package com.snail.permission.fileProvider;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.util.Log;

import com.snail.permission.BuildConfig;

import java.io.File;
import java.util.List;

/**
 * FileProvider工具类
 *
 * @author zhaoyan
 * @date 2019-02-25
 */
public class SnailFileProviderUtils {

    public static final String IMG_TYPE = "image/*";
    public static final String INSTALL_APK_DATA_TYPE = "application/vnd.android.package-archive";
    private static final String TAG = SnailFileProviderUtils.class.getSimpleName();

    /**
     * 获取file对应的uri, 需要读写权限的
     *
     * @param context
     * @param intent
     * @param file
     * @param writeAble
     * @return
     */
    public static Uri getUriForFileWithPermission(Context context, Intent intent, File file, boolean writeAble) {
        Uri fileUri = null;
        if (null == file) {
            return fileUri;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            //Android 7以上系统
            fileUri = getUriForFileAndroidN(context, file);
            grantPermissions(context, intent, fileUri, writeAble);
        } else {
            //7.0以下系统还用旧的访问方式
            fileUri = Uri.fromFile(file);
        }
        return fileUri;
    }

    /**
     * 获取file对应的uri, 不需要读写权限的
     *
     * @param context
     * @param file
     * @return
     */
    public static Uri getUriForFile(Context context, File file) {
        Uri fileUri = null;
        if (null == file) {
            return fileUri;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            //Android 7以上系统
            fileUri = getUriForFileAndroidN(context, file);
        } else {
            //7.0以下系统还用旧的访问方式
            fileUri = Uri.fromFile(file);
        }
        return fileUri;
    }

    /**
     * 获取file对应的uri(android 7系统以上)
     *
     * @param context
     * @param file
     * @return
     */
    private static Uri getUriForFileAndroidN(Context context, File file) {
        if (null == file) {
            return null;
        }
        String pkgName = context.getPackageName();
        Uri fileUri = SnailFileProvider.getUriForFile(context, pkgName + ".snailfileprovider",
                file);
        if (BuildConfig.DEBUG) {
            Log.d(TAG, "getUriForFileAndroidN pkName = " + pkgName + ", fileUri = " + fileUri);
        }
        return fileUri;
    }

    /**
     * intent设置uri
     *
     * @param context
     * @param intent
     * @param type
     * @param file
     * @param writeAble
     */
    public static void setIntentDataAndType(Context context, Intent intent, String type, File file,
                                            boolean writeAble) {
        if (null == file) {
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Uri uri = getUriForFileAndroidN(context, file);
            intent.setDataAndType(uri, type);
            grantPermissions(context, intent, uri, writeAble);
        } else {
            intent.setDataAndType(Uri.fromFile(file), type);
        }
    }

    /**
     * 设置intent data
     *
     * @param context
     * @param intent
     * @param file
     * @param writeAble
     */
    public static void setIntentData(Context context, Intent intent, File file, boolean writeAble) {
        if (null == file) {
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Uri uri = getUriForFileAndroidN(context, file);
            intent.setData(uri);
            grantPermissions(context, intent, uri, writeAble);
        } else {
            intent.setData(Uri.fromFile(file));
        }
    }

    /**
     * 授权读写权限
     *
     * @param context
     * @param intent
     * @param uri
     * @param writeAble
     */
    public static void grantPermissions(Context context, Intent intent, Uri uri, boolean writeAble) {
        int flag = Intent.FLAG_GRANT_READ_URI_PERMISSION;
        if (writeAble) {
            flag |= Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
        }
        intent.addFlags(flag);
        List<ResolveInfo> resInfoList = context.getPackageManager()
                .queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        for (ResolveInfo resolveInfo : resInfoList) {
            String packageName = resolveInfo.activityInfo.packageName;
            context.grantUriPermission(packageName, uri, flag);
        }
    }
}
